//
//  ManageController.m
//  MY Project For Sport
//
//  Created by admin on 16/2/11.
//  Copyright © 2016年 dilo. All rights reserved.
//

#import "ManageController.h"
#import "NetworkHandle.h"
#import "ModelForMatch.h"
#import "ManageCellTableViewCell.h"
#import "Manage2Controller.h"
#import <DKNightVersion.h>

#define WEIGHTT [UIScreen mainScreen].bounds.size.width / 375
#define HEIGHTT [UIScreen mainScreen].bounds.size.height / 667


@interface ManageController ()<NetworkHandleDelegate, UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UIView *MyView;
@property (nonatomic, retain) UIButton *button;
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *array;

@end

@implementation ManageController

- (void)dealloc {

    [_MyView release];
    [_button release];
    [_tableView release];
    [_array release];

    [super dealloc];

}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
   // self.navigationController.navigationBarHidden = YES;
    
    self.view.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor lightGrayColor]);
      
    [self handLeData];
    self.navigationItem.title = @"球队";
   // [self createTopTitle];
    [self createSearch];
    [self createTableView];
    
}


- (void)createSearch {

    UISearchBar *search = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 40 * HEIGHTT)];
    [self.view addSubview:search];
    [search release];
    search.placeholder = @"搜索";
    
}

- (void)handLeData {

    
    NSString *str = @"http://interface.sina.cn/sports/sports_navs/client_sports_ctrl/client_sports_ctrl.d.json";
    
    [NetworkHandle handlerJSONWitURL:str delegate:self];


}

- (void)handleDidComplete:(id)result {

    self.array = [NSMutableArray array];
    NSDictionary *dic = [result objectForKey:@"result"];
    
    NSDictionary *dic2 = [dic objectForKey:@"data"];
    
    NSArray *arr = [dic2 objectForKey:@"list"];
    
    for (NSDictionary *dicT in arr) {
        ModelForMatch *model = [[ModelForMatch alloc] init];
        [model setValuesForKeysWithDictionary:dicT];
        
        if ([model.ID isEqualToString:@"4"] || [model.ID isEqualToString:@"10"] || [model.ID isEqualToString:@"nba"] || [model.ID isEqualToString:@"cba_1"] || [model.ID isEqualToString:@"2"] || [model.ID isEqualToString:@"3"] || [model.ID isEqualToString:@"1"] || [model.ID isEqualToString:@"5"]|| [model.ID isEqualToString:@"213"] || [model.ID isEqualToString:@"328"]) {
            
            [self.array addObject:model];
        }
        
        
        [model release];
    }
    
    [self.tableView reloadData];

}


- (void)createTableView {

    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64 + 40 * HEIGHTT, self.view.frame.size.width , self.view.frame.size.height - 113 - 40 * HEIGHTT) style:UITableViewStylePlain];
    
    [self.view addSubview:self.tableView];
    [_tableView release];

    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    
    [self.tableView registerClass:[ManageCellTableViewCell class] forCellReuseIdentifier:@"poolForManage"];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

          return self.array.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ManageCellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForManage"];
    
    ModelForMatch *model = [self.array objectAtIndex:indexPath.row];
    
    [cell passModelForManage:model];
    
    return cell;
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {


    return self.view.frame.size.height / 8;



}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {


    Manage2Controller *manage = [[Manage2Controller alloc] init];
    
    ModelForMatch *model = [self.array objectAtIndex:indexPath.row];
    
    //NSLog(@"%@", model.ID);
    
    if ([model.ID isEqualToString:@"nba"]) {
        
        manage.url = @"http://platform.sina.com.cn/sports_all/client_api?app_key=2586208540&_version__=3.2.1.01&__os__=android&_sport_t_=basketball&_sport_s_=nba&_sport_a_=getteams&type=nba";
        
    } else if ([model.ID isEqualToString:@"cba_1"]) {
    
    manage.url = @"http://platform.sina.com.cn/sports_all/client_api?app_key=2586208540&_version__=3.2.1.01&__os__=android&_sport_t_=basketball&_sport_s_=cba&_sport_a_=getteams&type=cba_1";
    
    
    
    }else {
    
        NSString *str = @"http://platform.sina.com.cn/sports_all/client_api?app_key=2586208540&_version__=3.2.1.01&__os__=android&_sport_t_=football&_sport_s_=opta&_sport_a_=getteams&type=";
        
        manage.url = [str stringByAppendingFormat:@"%@", model.ID];
        
    
    }
    
    manage.name = model.title;
    [self.navigationController pushViewController:manage animated:YES];

    [manage release];



}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
